ParametrizeCamera_AreaScanIOSettings.py

Configure IO Settings of Area Scan Cameras

It shows how to configure trigger node IO settings of area scan cameras.

1 # -- coding: utf-8 --
2 
3 import sys
4 import platform
5 import os
6 
7 
8 # Compatible with different operating systems to load DDL
9 currentsystem = platform.system()
10 if currentsystem == 'Windows':
11  sys.path.append(os.path.join(os.getenv('MVCAM_COMMON_RUNENV'), "Samples", "Python", "MvImport"))
12 else:
13  sys.path.append(os.path.join("..", "..", "MvImport"))
14 
15 from MvCameraControl_class import *
16 
17 
18 # Compatible with input processing of Python 2.X and 3.X
19 if sys.version_info[0] < 3:
20  # Python 2.x
21  input_func = raw_input
22 else:
23  # Python 3.x
24  input_func = input
25 
26 # Decoding Characters
27 def decoding_char(ctypes_char_array):
28  """
29  Safely decode a string from a ctypes character array.
30  Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments.
31  """
32  byte_str = memoryview(ctypes_char_array).tobytes()
33 
34  # Truncate at the first null character
35  null_index = byte_str.find(b'\x00')
36  if null_index != -1:
37  byte_str = byte_str[:null_index]
38 
39  # Attempt to decode using multiple encodings
40  for encoding in ['gbk', 'utf-8', 'latin-1']:
41  try:
42  return byte_str.decode(encoding)
43  except UnicodeDecodeError:
44  continue
45 
46  # If all encodings fail, use a replacement strategy
47  return byte_str.decode('latin-1', errors='replace')
48 
49 fun_ctype = CFUNCTYPE
50 
51 stFrameInfo = POINTER(MV_FRAME_OUT_INFO_EX)
52 pData = POINTER(c_ubyte)
53 FrameInfoCallBack = fun_ctype(None, pData, stFrameInfo, c_void_p)
54 
55 def image_callback(pData, pFrameInfo, pUser):
56  stFrameInfo = cast(pFrameInfo, POINTER(MV_FRAME_OUT_INFO_EX)).contents
57  if stFrameInfo:
58  print("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))
59 
60 CALL_BACK_FUN = FrameInfoCallBack(image_callback)
61 
62 if __name__ == "__main__":
63 
64  try:
65  # Initialize SDK resources
66  MvCamera.MV_CC_Initialize()
67 
68  SDKVersion = MvCamera.MV_CC_GetSDKVersion()
69  print ("SDKVersion[0x%x]" % SDKVersion)
70 
71  deviceList = MV_CC_DEVICE_INFO_LIST()
72  tlayerType = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
73  | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
74 
75  # Enumerate devices
76  ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
77  if ret != 0:
78  print("enum devices fail! ret[0x%x]" % ret)
79  sys.exit()
80 
81  if deviceList.nDeviceNum == 0:
82  print("find no device!")
83  sys.exit()
84 
85  print("Find %d devices!" % deviceList.nDeviceNum)
86 
87  for i in range(0, deviceList.nDeviceNum):
88  mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
89  if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
90  print("\ngige device: [%d]" % i)
91  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
92  print("device model name: %s" % strModeName)
93 
94  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
95  print("device serial number: %s" % strSerialNumber)
96 
97  nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
98  nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
99  nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
100  nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
101  print("current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
102  elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
103  print("\nu3v device: [%d]" % i)
104  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
105  print("device model name: %s" % strModeName)
106 
107  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
108  print("device serial number: %s" % strSerialNumber)
109  elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
110  print("\nCML device: [%d]" % i)
111  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
112  print("device model name: %s" % strModeName)
113 
114  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
115  print("device serial number: %s" % strSerialNumber)
116  elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
117  print("\nCXP device: [%d]" % i)
118  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
119  print("device model name: %s" % strModeName)
120 
121  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
122  print("device serial number: %s" % strSerialNumber)
123  elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
124  print("\nXoF device: [%d]" % i)
125  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
126  print("device model name: %s" % strModeName)
127 
128  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
129  print("device serial number: %s" % strSerialNumber)
130 
131  nConnectionNum = input_func("please input the number of the device to connect:")
132 
133  if int(nConnectionNum) >= deviceList.nDeviceNum:
134  print("input error!")
135  sys.exit()
136 
137  # Create a camera instance
138  cam = MvCamera()
139 
140  # Select a device, and create a handle
141  stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
142 
143  ret = cam.MV_CC_CreateHandle(stDeviceList)
144  if ret != 0:
145  raise Exception("create handle fail! ret[0x%x]" % ret)
146 
147  # Turn on the device
148  ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
149  if ret != 0:
150  raise Exception("open device fail! ret[0x%x]" % ret)
151 
152  # Get optimal packet size (only supported by GigE devices)
153  if stDeviceList.nTLayerType == MV_GIGE_DEVICE or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
154  nPacketSize = cam.MV_CC_GetOptimalPacketSize()
155  if int(nPacketSize) > 0:
156  ret = cam.MV_CC_SetIntValue("GevSCPSPacketSize", nPacketSize)
157  if ret != 0:
158  print("Warning: Set Packet Size fail! ret[0x%x]" % ret)
159  else:
160  print("Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
161 
162  # Set Line0 as the IO input. Line2 is also available with a lower delay
163  print("Now set IO input...")
164  # Set trigger mode to on
165  ret = cam.MV_CC_SetEnumValueByString("TriggerMode", "On")
166  if ret != 0:
167  raise Exception("set trigger mode on fail! ret[0x%x]" % ret)
168 
169  # Set trigger source to Line0
170  ret = cam.MV_CC_SetEnumValueByString("TriggerSource", "Line0")
171  if ret != 0:
172  raise Exception("set trigger source fail! ret[0x%x]" % ret)
173 
174  # Set trigger edge (TriggerActivation) to RisingEdge
175  ret = cam.MV_CC_SetEnumValueByString("TriggerActivation", "RisingEdge")
176  if ret != 0:
177  raise Exception("set trigger activation fail! ret[0x%x]" % ret)
178 
179  # Set trigger delay time
180  ret = cam.MV_CC_SetFloatValue("TriggerDelay", 0)
181  if ret != 0:
182  raise Exception("set trigger delay fail! ret[0x%x]" % ret)
183 
184  # Disable TriggerCacheEnable, and you can enable it according to the actual demand
185  ret = cam.MV_CC_SetBoolValue("TriggerCacheEnable", False)
186  if ret != 0:
187  raise Exception("set trigger cache enable fail! ret[0x%x]" % ret)
188 
189  # Set LineSelector to Line0
190  ret = cam.MV_CC_SetEnumValueByString("LineSelector", "Line0")
191  if ret != 0:
192  raise Exception("set line selector fail! ret[0x%x]" % ret)
193 
194  # Set the filtering time (us) for Line0. Increase it appropriately if there are false triggers
195  ret = cam.MV_CC_SetIntValueEx("LineDebouncerTime", 50)
196  if ret != 0:
197  raise Exception("set line debouncer time fail! ret[0x%x]" % ret)
198 
199  # Set Line1 as the IO output. Line2 is also available with a lower delay. This is used to control external light source and other devices
200  print("Now set IO output...")
201  # Set LineSelector to Line1
202  ret = cam.MV_CC_SetEnumValueByString("LineSelector", "Line1")
203  if ret != 0:
204  raise Exception("set line selector fail! ret[0x%x]" % ret)
205 
206  # Set LineSource to ExposureStartActive
207  ret = cam.MV_CC_SetEnumValueByString("LineSource", "ExposureStartActive")
208  if ret != 0:
209  raise Exception("set line source fail! ret[0x%x]" % ret)
210 
211  # Enable StrobeEnable
212  ret = cam.MV_CC_SetBoolValue("StrobeEnable", True)
213  if ret != 0:
214  raise Exception("set strobe enable fail! ret[0x%x]" % ret)
215 
216  # Set duration of output channel (StrobeLineDuration), unit: us
217  ret = cam.MV_CC_SetIntValueEx("StrobeLineDuration", 0)
218  if ret != 0:
219  raise Exception("set strobe line duration fail! ret[0x%x]" % ret)
220 
221  # Set the delay time of output line (StrobeLineDelay), unit: us
222  ret = cam.MV_CC_SetIntValueEx("StrobeLineDelay", 0)
223  if ret != 0:
224  raise Exception("set strobe line delay fail! ret[0x%x]" % ret)
225 
226  # Set pre-delay time of output channel (StrobeLinePreDelay), unit: us
227  ret = cam.MV_CC_SetIntValueEx("StrobeLinePreDelay", 0)
228  if ret != 0:
229  raise Exception("set strobe line pre-delay fail! ret[0x%x]" % ret)
230 
231  #Register an image grabbing callback
232  ret = cam.MV_CC_RegisterImageCallBackEx(CALL_BACK_FUN, None)
233  if ret != 0:
234  raise Exception("register image callback fail! ret[0x%x]" % ret)
235 
236  # Start grabbing images
237  ret = cam.MV_CC_StartGrabbing()
238  if ret != 0:
239  raise Exception("start grabbing fail! ret[0x%x]" % ret)
240  else:
241  print("start grabbing success")
242 
243  print ("press Enter key to stop grabbing.")
244  input_func()
245 
246  # Stop grabbing images
247  ret = cam.MV_CC_StopGrabbing()
248  if ret != 0:
249  raise Exception("stop grabbing fail! ret[0x%x]" % ret)
250 
251  # Turn off the device
252  ret = cam.MV_CC_CloseDevice()
253  if ret != 0:
254  raise Exception("close device fail! ret[0x%x]" % ret)
255 
256  # Destroy the handle
257  cam.MV_CC_DestroyHandle()
258 
259  except Exception as e:
260  print(e)
261  cam.MV_CC_CloseDevice()
262  cam.MV_CC_DestroyHandle()
263  finally:
264  # Release SDK resources
265  MvCamera.MV_CC_Finalize()